package nku.csc364.updatedatabase;

import nku.csc364.R;
import nku.csc364.databasehelper.DataHelper;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class UpdateDatabaseSupplierActivity extends Activity{
	
	Spinner supplierSupplier;
	EditText supplierName, supplierStatus, supplierAdr1, supplierAdr2, supplierCity, supplierState,
				supplierZip, supplierPhone;
	DataHelper dh = null; 
	
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.supplier);
        
        supplierSupplier = (Spinner)findViewById(R.id.supplierSpinner1);
        supplierName = (EditText)findViewById(R.id.supplierEditText1);
        supplierStatus = (EditText)findViewById(R.id.supplierEditText2);
        supplierAdr1 = (EditText)findViewById(R.id.supplierEditText3);
        supplierAdr2 = (EditText)findViewById(R.id.supplierEditText4);
        supplierCity = (EditText)findViewById(R.id.supplierEditText5);
        supplierState = (EditText)findViewById(R.id.supplierEditText6);
        supplierZip = (EditText)findViewById(R.id.supplierEditText7);
        supplierPhone = (EditText)findViewById(R.id.supplierEditText8);
        
        dh = DataHelper.getDataHelper(this);
        setSupplierSpinner();
        supplierSupplier.setPrompt("Select...");
    }
    
	private void setSupplierSpinner() {
		supplierSupplier.setAdapter(getSupplierAdapter());
	}

	  @SuppressWarnings("unchecked")
		public ArrayAdapter<CharSequence> getSupplierAdapter(){
				    @SuppressWarnings("rawtypes")
					ArrayAdapter itemAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, dh.selectAllSuppliers());	 
			        itemAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			        itemAdapter.insert(" ", 0);
			        return itemAdapter;
		}
	  
	  public class MyOnItemSelectedListener implements OnItemSelectedListener {
		    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
		    	if(!parent.getItemAtPosition(pos).toString().equals(" ")){
		    		Cursor mCursor = dh.getItem(parent.getItemAtPosition(pos).toString());
			    	int i = 0;
					for(String x : mCursor.getColumnNames()){
						if(i == 0){
							supplierName.setText(mCursor.getString(mCursor.getColumnIndexOrThrow(x)));
						}
						else if( i == 1){
							supplierStatus.setText(mCursor.getString(mCursor.getColumnIndexOrThrow(x)));
						}
						else if ( i == 2){
							supplierAdr1.setText(mCursor.getString(mCursor.getColumnIndexOrThrow(x)));
						}
						else if ( i == 3){
							supplierAdr2.setText(mCursor.getString(mCursor.getColumnIndexOrThrow(x)));
						}
						else if(i == 4){
							supplierCity.setText(mCursor.getString(mCursor.getColumnIndexOrThrow(x)));
						}
						else if( i == 5){
							supplierState.setText(mCursor.getString(mCursor.getColumnIndexOrThrow(x)));
						}
						else if ( i == 6){
							supplierZip.setText(mCursor.getString(mCursor.getColumnIndexOrThrow(x)));
						}
						else if ( i == 7){
							supplierPhone.setText(mCursor.getString(mCursor.getColumnIndexOrThrow(x)));
						}
						i++;
					}
			    }
		    	else{
		    		clean();
		    	}
		    }
		    public void onNothingSelected(@SuppressWarnings("rawtypes") AdapterView parent) {
		      // Do nothing.
		    }
	}
	  
	private void clean(){
		supplierName.setText(""); 
		supplierStatus.setText(""); 
		supplierAdr1.setText(""); 
		supplierAdr2.setText(""); 
		supplierCity.setText(""); 
		supplierState.setText("");
		supplierZip.setText(""); 
		supplierPhone.setText("");
	}
	
	public  void openCategory(View view){
		Intent launchCategory = new Intent(this, UpdateDatabaseCategoryActivity.class);
		startActivityForResult(launchCategory, 1);
	}
	
	public void openItem(View view){
		Intent launchItem = new Intent(this, UpdateDatabaseItemActivity.class);
		startActivityForResult(launchItem, 1);
	}
	
	public void openProduct(View view){
		Intent launchProduct = new Intent(this, UpdateDatabaseProductActivity.class);
		startActivityForResult(launchProduct, 1);
	}
	
	public void openQOH(View view){
		Intent launchQOH = new Intent(this, UpdateDatabaseQOHActivity.class);
		startActivityForResult(launchQOH, 1);
	}
	
	public void openSupplier(View view){
		Intent launchSupplier = new Intent(this, UpdateDatabaseSupplierActivity.class);
		startActivityForResult(launchSupplier, 1);
	}
    
    public void doAddSupplier(View view){
    	dh.insertSupplier(this.supplierName.getText().toString(), 
    			this.supplierStatus.getText().toString(), 
    			this.supplierAdr1.getText().toString(), 
    			this.supplierAdr2.getText().toString(), 
    			this.supplierCity.getText().toString(), 
    			this.supplierState.getText().toString(), 
    			this.supplierZip.getText().toString(), 
    			this.supplierPhone.getText().toString());
    }
    
    public void doDeleteSupplier(View view){
    	dh.deleteSupplier(this.supplierName.getText().toString());
    }
    
    public void doUpdateSupplier(View view){
    	doAddSupplier(view);
    	doDeleteSupplier(view);
    }
}
